package com.ruoyi.web.controller.system;

import com.google.common.collect.Lists;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审批Controller
 * 
 * @author ruoyi
 * @date 2020-04-10
 */
@Controller
@RequestMapping("/system/approval")
public class ApprovalController extends BaseController
{
    private String prefix = "system/approval";

    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private ISysDeptService sysDeptService;


    @Autowired
    private IWritingsService writingsService;
    @Autowired
    private IAppraisalService appraisalService;
    @Autowired
    private IContractService contractService;
    @Autowired
    private IPaperService paperService;
    @Autowired
    private IPatentService patentService;
    @Autowired
    private IPrizeService prizeService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISoftwareService softwareService;
    @Autowired
    private IAppendixService appendixService;

    @RequiresPermissions("system:approval:view")
    @GetMapping()
    public String approval()
    {
        return prefix + "/approval";
    }

    /**
     * 查询审批列表
     */
    @RequiresPermissions("system:approval:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Approval approval)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        List<Approval> list = approvalService.selectApprovalList(approval,sysUser);
        return getDataTable(list);
    }

    /**
     * 导出审批列表
     */
    @RequiresPermissions("system:approval:export")
    @Log(title = "审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Approval approval)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        List<Approval> list = approvalService.selectApprovalList(approval, sysUser);
        ExcelUtil<Approval> util = new ExcelUtil<Approval>(Approval.class);
        return util.exportExcel(list, "approval");
    }


    /**
     * 审批
     */
    @RequiresPermissions("system:approval:audit")
    @Log(title = "审批", businessType = BusinessType.DELETE)
    @PostMapping( "/audit")
    @ResponseBody
    public AjaxResult audit( Long id,Integer auditState,String remark)
    {
        approvalService.audit(id,auditState,remark,ShiroUtils.getSysUser());
        return  toAjax(true);
    }


    /**
     * 审批页面
     */
    @GetMapping( "/audit/{id}")
    public String toAudit(@PathVariable Long id,ModelMap mmap)
    {
        Approval approval = approvalService.selectApprovalById(id);
        Integer docType = approval.getDocType();
        DocTypeEnum docTypeEnum = EnumUtil.getIEnum(DocTypeEnum.class,docType);
        Long docRelationId = approval.getDocRelationId();
        String redirectUrl = "appraisalAudit";
        Object doc = null;
        List<Appendix> appendices = Lists.newArrayList();
        switch (docTypeEnum) {
            case PAPER:
                doc = paperService.selectPaperById(docRelationId);
               redirectUrl = "/paperAudit";
                break;
            case PRIZE:
                doc = prizeService.selectPrizeById(docRelationId);
                redirectUrl = "/prizeAudit";
                break;
            case PATENT:
                doc = patentService.selectPatentById(docRelationId);
                redirectUrl = "/patentAudit";
                break;
            case REPORT:
                doc = reportService.selectReportById(docRelationId);
                redirectUrl = "/reportAudit";
                break;
            case CONTRACT:
                doc = contractService.selectContractById(docRelationId);
                redirectUrl = "/contractAudit";
                break;
            case SOFTWARE:
                doc = softwareService.selectSoftwareById(docRelationId);
                redirectUrl = "/softwareAudit";
                break;
            case WRITINGS:
                doc = writingsService.selectWritingsById(docRelationId);
                redirectUrl = "/writingsAudit";
                break;
            case APPRAISAL:
                doc = appraisalService.selectTAppraisalById(docRelationId);
                redirectUrl = "/appraisalAudit";
                break;
        }
        mmap.put("doc",doc);
        appendices = appendixService.listByDocTypeAndId(docRelationId,docType);
        mmap.put("appendices",appendices);
        mmap.put("approvalId",approval.getId());
        return  prefix + redirectUrl;
    }
}
